package org.hank.demoproject.smarttable

import android.os.Bundle
import android.view.LayoutInflater
import androidx.lifecycle.lifecycleScope
import com.bin.david.form.core.SmartTable
import com.bin.david.form.data.column.ArrayColumn
import com.bin.david.form.data.column.Column
import com.bin.david.form.data.format.IFormat
import com.bin.david.form.data.table.TableData
import com.orhanobut.logger.Logger
import com.yyzs.measuremerchant.bean.measure.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.hank.demoproject.BaseViewBindingActivity
import org.hank.demoproject.databinding.ActivitySmarttableBinding
import java.math.BigDecimal

/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2023/4/18 10:12
 *描述:
 */
class SmartTableActivity : BaseViewBindingActivity<ActivitySmarttableBinding>() {
    lateinit var smartTable: SmartTable<ThreeSideTwoLine>
    var threeSideTwoLine: MutableList<ThreeSideTwoLine> = ArrayList()

    init {

    }

    override fun inflateLayout(layoutInflater: LayoutInflater): ActivitySmarttableBinding {
        return ActivitySmarttableBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        smartTable = viewBinding.smartTable as SmartTable<ThreeSideTwoLine>

        smartTable.config.apply {
            minTableWidth = this@SmartTableActivity.resources.displayMetrics.widthPixels
            verticalPadding = 44
        }
        var measureInfo1 = MeasureInfo(3, 0, BigDecimal(100), 10)
        var measureInfo2 = MeasureInfo(3, 1, BigDecimal(99), 11)
        var measureInfo3 = MeasureInfo(3, 2, BigDecimal(98), 12)
        var door = Door(
            "222",
            12,
            mutableListOf(measureInfo1, measureInfo2, measureInfo3),
            BigDecimal(100),
            10
        )
        var skirt = Skirt(
            "222",
            13,
            mutableListOf(measureInfo1, measureInfo2, measureInfo3),
            BigDecimal(99),
            6
        )
        var socket = Socket(
            "222",
            14,
            mutableListOf(measureInfo1, measureInfo2, measureInfo3),
            BigDecimal(98),
            7
        )
        var threeSideTwoLine1 = ThreeSideTwoLine(
            mutableListOf("mesh", "ply"), mutableListOf(door, door), mutableListOf(skirt, skirt),
            mutableListOf(socket, socket), 0, 9, BigDecimal(100), 100
        )
        var threeSideTwoLine2 = ThreeSideTwoLine(
            mutableListOf("mesh", "ply"), mutableListOf(door, door), mutableListOf(skirt, skirt),
            mutableListOf(socket, socket), 1, 9, BigDecimal(100), 100
        )
        threeSideTwoLine.add(threeSideTwoLine1)
        threeSideTwoLine.add(threeSideTwoLine2)

        var wallColumn = Column<Any>("墙面序号", "wall_index")
        var door2dColumn = ArrayColumn<Any>("3D/2D图", "door.display_2D_map")
        var measureInfo1Door = ArrayColumn<Any>("测量信息1", "door.measure_info.label_index")
        var measureInfo2Door = ArrayColumn<Any>("测量信息2", "door.measure_info.score")
        var measureInfo3Door = ArrayColumn<Any>("测量信息3", "door.measure_info.total_scanned_points")
        var skirt2dColumn = ArrayColumn<Any>("3D/2D图", "skirt.display_2D_map")
        var measureInfo1skirt = ArrayColumn<Any>("测量信息1", "skirt.measure_info.label_index")
        var measureInfo2skirt = ArrayColumn<Any>("测量信息2", "skirt.measure_info.score")
        var measureInfo3skirt = ArrayColumn<Any>("测量信息3", "skirt.measure_info.total_scanned_points")
        var socket2dColumn = ArrayColumn<Any>("3D/2D图", "socket.display_2D_map")
        var measureInfo1socket = ArrayColumn<Any>("测量信息1", "socket.measure_info.label_index")
        var measureInfo2socket = ArrayColumn<Any>("测量信息2", "socket.measure_info.score")
        var measureInfo3socket =
            ArrayColumn<Any>("测量信息3", "socket.measure_info.total_scanned_points")


        var columnList: MutableList<Column<Any>> = ArrayList()
        columnList.add(wallColumn)
        columnList.add(door2dColumn)
        columnList.add(measureInfo1Door)
        columnList.add(measureInfo2Door)
        columnList.add(measureInfo3Door)
        columnList.add(skirt2dColumn)
        columnList.add(measureInfo1skirt)
        columnList.add(measureInfo2skirt)
        columnList.add(measureInfo3skirt)
        columnList.add(socket2dColumn)
        columnList.add(measureInfo1socket)
        columnList.add(measureInfo2socket)
        columnList.add(measureInfo3socket)

        var tempTableData = TableData("", threeSideTwoLine, columnList)
        smartTable.tableData = tempTableData
        smartTable.post {
            lifecycleScope.launch(Dispatchers.IO) { Logger.d("total line size ${smartTable.tableData.tableInfo.arrayLineSize.sum()}") }
        }
        smartTable.setZoom(true)
        door2dColumn.setOnColumnItemClickListener { column, value, t, position ->
            Logger.d("position == $position node name${ (column as ArrayColumn).node.parent}")
        }
    }
}